<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/23
 * Time: 11:35
 */
namespace Home\Controller;

class UserController extends BaseController {

    public function index(){

        $userId = session('user_id');

        $where = [];
        if(session('auth') != 'admin' && $userId>0) $where['partner_id'] = $userId;
        $where['auth'] = ['neq','admin'];
        $User = M('User');
        $userList = $User->where($where)->select();

        foreach($userList as &$item){
            $item['partner_name'] = $User->where(['id'=>$item['partner_id']])->getField('real_name');
        }

        $breadCrumbs = ['权限管理'];
        $this->assign(compact('userList','breadCrumbs'));
        $this->display();
    }

    public function myEdit(){
        $userId = session('user_id');
        $User = M('User');
        $userList = $User->field('id,real_name')->select();

        $info = $User->find($userId);

        $breadCrumbs = ['权限管理','修改信息'];
        $this->assign(compact('userList','info','breadCrumbs'));
        $this->display();
    }


    public function userAdd(){
        $userId = session('user_id');

        $where = [];
        if(session('auth') != 'admin' && $userId>0) $where['id'] = $userId;
        $where['auth'] = ['neq','admin'];
        $User = M('User');
        $userList = $User->field('id,real_name')->where($where)->select();

        $breadCrumbs = ['权限管理','添加用户'];
        $this->assign(compact('userList','breadCrumbs'));
        $this->display();
    }

    public function userSave(){
        $User = D('User');
        $input = I('post.');

        if(session('auth') != 'admin' && $input['partner_id'] != session('user_id')) $this->error('所属上级必须选择自己！');

        //检测用户名不能重复
        $existName = $User->where(['user_name'=>$input['user_name']])->find();
        if($existName) $this->error('用户名已经存在，请重新输入');

        //检测目录是否已经存在
        $existDir = $User->where(['dir_name'=>$input['dir_name']])->find();
        if($existDir) $this->error('目录名称已经存在，请重新输入');

        $User->create($input);
        $User->password =  md5($User->password);
        $res = $User->add();

        if(!$res) $this->error('创建失败！');

        //插件文件加

        $path = './Public/files/'.$input['dir_name'];
        if (!file_exists($path)) {
            mkdir($path,0777,true);
        }else{
            $this->error('文件加已经存在');
        }




        $this->success('创建成功！',U('User/index'));
    }

    public function userEdit(){
        $userId = session('user_id');
        $id = I('get.id',0,'intval');
        $User = M('User');
        if(session('auth') != 'admin' && $userId>0) $where['id'] = $userId;
        $where['auth'] = ['neq','admin'];
        $userList = $User->field('id,real_name')->where($where)->select();

        $info = $User->find($id);

        $breadCrumbs = ['权限管理','编辑用户'];
        $this->assign(compact('userList','info','breadCrumbs'));
        $this->display();
    }

    public function userUpdate(){
        $User = D('User');
        $input = I('post.');

        if($input['partner_id']){
            if($input['partner_id'] == $input['id']) $this->error('所属上级不能为自己');
            if(session('auth') != 'admin' && $input['partner_id'] != session('user_id')) $this->error('所属上级必须选择自己！');
        }


        //检测用户名不能重复
        $existName = $User->where(['user_name'=>$input['user_name'],['id'=>['neq',$input['id']]]])->find();
        if($existName) $this->error('用户名已经存在，请重新输入');

        //检测目录是否已经存在
        $existDir = $User->where(['dir_name'=>$input['dir_name'],['id'=>['neq',$input['id']]]])->find();
        if($existDir) $this->error('目录名称已经存在，请重新输入');

        //检测文件夹是否存在
        $oldDir = $User->field('dir_name')->find($input['id']);

        $oldPath = './Public/files/'.$oldDir['dir_name'];
        if(!file_exists($oldPath) && (session('auth') != 'admin')) $this->error('旧文件夹不存在');

        $newPath = './Public/files/'.$input['dir_name'];
        if (file_exists($newPath) && $oldPath != $newPath && $input['dir_name']) $this->error('文件夹已经存在');

        if(!empty($input['repassword'])) $input['password'] = md5($input['repassword']);
        $User->create($input);

        $res = $User->save();

        if($res === false) $this->error('保存失败！');


        rename($oldPath,$newPath);

        $this->success('保存成功！',U('User/index'));
    }

    public function userDel(){
        $id = I('id',0,'intval');

        $User = M('User');
        $res = $User->delete($id);

        if($res === false) $this->error('删除失败！');

        $this->success('删除成功！');

    }
}